<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Store;
use App\Models\Categorys;
use App\Models\Stores;
use Illuminate\Http\Request;

class StoreController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        //获取分类
        $categorys = Categorys::getCascaderList();
        $categorys = json_encode($categorys);
        return view('admin.store.index',compact('categorys'));
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'recommend' => (int)$request->get('recommend',0),
                'status' => (int)$request->get('status',0),
                'cid' => (int)$request->get('cid',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
                'sort' => (string)$request->get('sort',''),
            ];
            $lists = Stores::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 编辑
     * @param Request $request
     * @return bool|view
     */
    public function edit(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $info = [];
            $pidTree=[];
            $date = [];
            if($id){
                $info = Stores::findOrFail($id);
                //将一级分类和二级分类拼接
                if(!empty($info['cate_id']) && !empty($info['cate_pid'])){
                    $info['ids'] = $info['cate_id'].",".$info['cate_pid'];
                }else{
                    if(!empty($info['cate_id'])){
                        $info['ids'] = $info['cate_id'];
                    }elseif (!empty($info['cate_pid'])){
                        $info['ids'] = $info['cate_pid'];
                    }
                }
                $info['ids'] = !empty($info['ids']) ? explode(',',$info['ids']) : 0;

                foreach ($info['ids'] as $k => $v){
                    $pid=Categorys::getPid($v);
                    if($pid > 0){
                        $pidTree[$k][]="$pid";
                        $pidTree[$k][]=$v;
                    }else{
                        $pidTree[$k][]=$v;
                    }
                }
                $info['cate_id'] = $pidTree;

                $date = explode(' - ',$info['workingtime']);
            }
            $info = json_encode($info);
            $date = json_encode($date);

            //获取分类
            $categorys = Categorys::getCascaderList();
            $categorys = json_encode($categorys);

            return view('admin.store.store_update',compact('info','id','date','categorys'));
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 更新
     * @param Store $request
     * @param array_push 将字符串push成数组
     * @param array_unique 数组去重
     * @return bool
     */
    public function update(Store $request)
    {
        try{
            $data = $request->all();
            $str1 = [];
            $str2 = [];
            foreach ($data['cate_id'] as $k=>$v) {
                array_push($str1, $v[0]);
                if (isset($v[1])){
                    array_push($str2, $v[1]);
                }
            }
            $data['cate_pid'] = implode(",",array_unique($str1));
            $data['cate_id'] = implode(",",array_unique($str2));
            if($data['id']){
                $info = Stores::findOrFail($data['id']);
                $info->update($data);
                return $this->writeJson(0,null,'修改信息成功');
            }else{
                unset($data['id']);
                Stores::create($data);
                return $this->writeJson(0,null,'新增信息成功');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 拒绝
     * @param Request $request
     * @return bool
     */
    public function lock(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Stores::findOrFail($id);
            $info->update(['status' => Stores::STATUS_3]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 通过
     * @param Request $request
     * @return bool
     */
    public function active(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Stores::findOrFail($id);
            $info->update(['status' => Stores::STATUS_1]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 关店
     * @param Request $request
     * @return bool
     */
    public function close(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Stores::findOrFail($id);
            $info->update(['status' => Stores::STATUS_4]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(is_array($id)){
                $where['id'] = ['in',$id];
            }else{
                $where['id'] = $id;
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            Stores::where($where)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
